package com.qth.valet.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qth.valet.common.annotation.Release;
import com.qth.valet.common.enums.PropertyEnum;
import com.qth.valet.common.theadlocal.UserOrDriverIdHolder;
import com.qth.valet.common.utils.ResponseResult;
import com.qth.valet.common.utils.StatusConstants;
import com.qth.valet.entity.po.DriverRecharge;
import com.qth.valet.entity.po.Property;
import com.qth.valet.entity.property.Vip;
import com.qth.valet.entity.req.RechargeReq;
import com.qth.valet.service.DriverRechargeService;
import com.qth.valet.service.PropertyService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/qth/recharge")
@RequiredArgsConstructor
public class DriverRechargeController {

    private final DriverRechargeService driverRechargeService;

    private final PropertyService propertyService;

    /**
     * 司机充值
     */
    @PostMapping("/recharge")
    public ResponseResult<Object> pay(@Valid @RequestBody RechargeReq req) {
        return ResponseResult.success(driverRechargeService.recharge(req));
    }

    /**
     * 司机充值前调用此api判断是否有未完成的充值订单
     */
    @GetMapping("/checkRecharge")
    public ResponseResult<List<DriverRecharge>> checkRecharge() {
        // 检查是否有未支付的订单
        List<DriverRecharge> list = driverRechargeService.list(new LambdaQueryWrapper<DriverRecharge>()
                .eq(DriverRecharge::getDriverId, UserOrDriverIdHolder.getDriverId())
                .eq(DriverRecharge::getStatus, StatusConstants.DRIVER_PAY_STATUS_PENDING)
                .eq(DriverRecharge::getType, StatusConstants.DRIVER_RECHARGE_TYPE_COMMON) // 只查普通充值记录
        );
        return ResponseResult.success(list);
    }

    /**
     * 查询充值记录
     */
    @GetMapping("/page")
    public ResponseResult<IPage<DriverRecharge>> page(@RequestParam(defaultValue = "1") int pageNum,
                                                      @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseResult.success(driverRechargeService.page(UserOrDriverIdHolder.getDriverId(), pageNum, pageSize));
    }

    /**
     * 取消一个充值订单
     */
    @DeleteMapping("/cancel/{id}")
    public ResponseResult<Boolean> cancel(@PathVariable("id") String id) {
        LambdaUpdateWrapper<DriverRecharge> wrapper = new LambdaUpdateWrapper<DriverRecharge>()
                .eq(DriverRecharge::getId, id)
                .eq(DriverRecharge::getStatus, StatusConstants.DRIVER_PAY_STATUS_PENDING)
                .set(DriverRecharge::getStatus, StatusConstants.DRIVER_PAY_STATUS_CANCEL);
        boolean update = driverRechargeService.update(wrapper);
        return ResponseResult.success(update);
    }

    /**
     * 继续充值
     */
    @GetMapping("/continue/{id}")
    public ResponseResult<Object> continueRecharge(@PathVariable("id") String id) {
        return ResponseResult.success(driverRechargeService.continueRecharge(id));
    }

    /**
     * 查询开通会员需要多少钱
     */
    @GetMapping("/getVipPrice")
    public ResponseResult<Vip> getOpenMemberPrice() {
        // 查会员金额
        Property vipPropertyObject = propertyService.getById(PropertyEnum.VIP.getKey());
        Vip vip = (Vip) PropertyEnum.VIP.parse(vipPropertyObject.getTheValue());
        return ResponseResult.success(vip);
    }

}
